package com.neo.office.server.controller.system;

import com.neo.office.server.config.data.Result;
import com.neo.office.server.domain.entity.Role;
import com.neo.office.server.domain.entity.User;
import com.neo.office.server.service.RoleService;
import com.neo.office.server.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/system/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    @ApiOperation(value = "获取所有用户")
    @GetMapping("/")
    public List<User> getAllUser(String keywords){
        return userService.getAllUser(keywords);
    }

    @ApiOperation(value = "更新用户")
    @PutMapping("/")
    public Result updateAUser(@RequestBody User user){
        if (userService.updateById(user)){
            return Result.success("更新成功");
        }
        return Result.error("更新失败");
    }

    @ApiOperation(value = "删除用户")
    @DeleteMapping("/{id}")
    public Result deleteUser(@PathVariable Integer id){
        if (userService.removeById(id)){
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }

    @ApiOperation(value = "获取所有角色")
    @GetMapping("/roles")
    public List<Role> getAllRole(){
        return roleService.list();
    }

    @ApiOperation(value = "更新用户角色")
    @PutMapping("/role")
    public Result updateUserRole(Integer userId,Integer[] rids){
        return userService.updateUserRole(userId,rids);
    }

}
